package com.teamc.team.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.teamc.team.entity.User;
import com.teamc.team.response.ResponseData;
import com.teamc.team.service.ILinkService;
import com.teamc.team.service.IUserService;
import com.teamc.team.util.ResponseDataUtil;
import com.teamc.team.vo.AdminUserVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author ding
 */
@Controller
public class AdminUserController {
    private final IUserService service;

    private final ILinkService linkService;

    public AdminUserController(IUserService service, ILinkService linkService) {
        this.service = service;
        this.linkService = linkService;
    }

    /**
     * 列出用户管理
     * @param no
     * @return
     */
    @RequestMapping("/admin/user")
    public String adminIndex(Model no) {
        no.addAttribute("list",service.findAllWithClass());
        return "admin/readers";
    }

    /**
     * 根据id查找用户
     * @param id
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping("/admin/findUserByID/{id}")
        @ResponseBody
    public ResponseData findUserByID(@PathVariable("id") String id) throws JsonProcessingException {
        return ResponseDataUtil.buildSuccess(service.findAllWithClassByID(id));
    }

    /**
     * 通过修改权限的逻辑删除用户
     * @param id
     * @return
     */
    @GetMapping("/admin/changeUserEnable/{id}")
    @ResponseBody
    public ResponseData changeUserEnableById(@PathVariable("id") String id) {
        try {
            service.ChangeUserEnable(id);
        } catch (Exception e) {
            return ResponseDataUtil.buildSuccess("修改用户权限请求失败");
        }
        return ResponseDataUtil.buildError("修改用户权限请求失败");
    }

    /**
     * 管理员删除用户方法 需提交对应的id
     * @param id
     * @return
     */
    @RequestMapping("/admin/deleteUserById/{id}")
    public String deleteUserById(@PathVariable("id") String id) {
        try {
            service.deleteUserById(id);
            return "redirect:/admin/user";
        } catch (Exception e) {
            return "redirect:/admin/user";
        }
    }

    /**
     * 管理员添加用户方法 需提交一个json
     * @param x
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/admin/addUser")
    @ResponseBody
    public ResponseData addUser(@RequestBody String x) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println(x);
        User user = objectMapper.readValue(x, User.class);
        System.out.println(user);
        try {
            service.addUser(user);
            return ResponseDataUtil.buildSuccess("添加成功！");
        } catch (Exception e) {
            return ResponseDataUtil.buildError("添加失败！");
        }
    }

    /**
     * 管理员修改用户方法
     * @param x
     * @return
     * @throws JsonProcessingException
     */
    @PostMapping("/admin/modifyUser/")
    public String modifyUser(User x) throws JsonProcessingException {
        try {
            service.modifyUser(x);
            return "redirect:/admin/user";
        } catch (Exception e) {
            return "redirect:/admin/user";
        }
    }

    @RequestMapping("/admin/user/edit/{id}")
    public String editUser(@PathVariable String id, Model mo) {
        try {
            AdminUserVO adminUserVO = service.findAllWithClassByID(id).get(0);
            mo.addAttribute("c",adminUserVO);
        } catch (Exception e) {
            return "redirect:/admin/user";
        }
        return "/admin/user_edit";
    }
}
